<?php
	
class Admin_ClientController extends TN_Controller_Action_Admin
{
 public function init()
    {
    	parent::init();
    }
    
 	public function indexAction()
 	{
 	  $lang = Common_Translate::getLanguage();
 	  $categories = new Zend_Models_LangClientCategory();
  	  $this->view->categories = $categories->getCategoriesByLang($lang);
 	}
 	
 	public function newcategoryAction()
 	{
 		$form = $this->addCategoryForm();
 		$this->view->form = $form;	
 		$langs = new Zend_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
    
    	if (!$this->getRequest()->isPost())
     	{
       		return;
     	}
     	else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$category = new Zend_Models_ClientCategory();
				$category = $category->createRow();
				$category->enabled = $values['common']['enabled'];
				$category->discount = $values['common']['discount'];
				$category->save();
				$langs = new Zend_Models_Lang();
				$langs = $langs->getEnabledLangs();
				foreach( $langs as $l )
				{
				  $langCategory = new Zend_Models_LangClientCategory();
				  $langCategory = $langCategory->createRow();
				  $langCategory->id_category = $category->id;
				  $langCategory->lang = $l['prefix2'];
				  $langCategory->name = $values[$l['prefix2']]['name_'.$l['prefix2']];
				  $langCategory->save();
				}
				
            	$this->_redirect("/admin/client");
 
			}
		}
 	}
 	
 	public function deletecategoryAction()
 	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$category = $request['category'];
    	$lc = new Zend_Models_LangClientCategory();
    	$lc->delete("id_category = '$category'"); 
    	$c = new Zend_Models_ClientCategory();
    	$c = $c->getCategoryById($category);
    	$c->delete();
     	$this->_redirect("/admin/client");
 	}
 
 	
 	private function addCategoryForm()
 	{
 	  $form = new Zend_Form();
 	  $form->setAction("/admin/client/newcategory")
 			->setMethod('post');
 	  $form->setAttrib("class", "jNice");
 	  /**** Datos comunes a los idiomas ****/   	
 	  $common = new Zend_Form_SubForm();  
 	  $enabled = $common->createElement('checkbox','enabled');
 	  $enabled->setLabel('Activo:');
	  $common->addElement($enabled);	
	  $discount = $common->createElement("text", "discount");
	  $discount->setAttrib("class", "text-long");
	  $discount->addValidator('Float')->setLabel('*% de descuento:')->setRequired(true);
	  $discount->addErrorMessage('Valor real invalido');
 	  $common->addElement($discount);
	  $form->addSubForms(array("common" => $common));
	  
      $langs = new Zend_Models_Lang();
	  $langs = $langs->getEnabledLangs();
	  foreach( $langs as $l)
	  {
	    $sub = new Zend_Form_SubForm();
	    $name = $sub->createElement("text","name_".$l['prefix2']);
		$name->setAttrib("class", "text-long");
 	   	$name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
		$name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$sub->addElement($name);
		$form->addSubForms(array($l['prefix2'] => $sub));
	  }
	  $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
      return $form;
 	}
 	
 	private function editCategoryForm()
 	{
 		$form = new Zend_Form();
 		$form->setAction("/admin/client/editcategory")
 				->setMethod('post');
 		$form->setAttrib("class", "jNice");		
 		$request = $this->_request->getParams();
 	    $category = $request['category']; 		
 	    $c = new Zend_Models_ClientCategory();
 	    $c = $c->getCategoryById($category);
 	    /***** Datos comunes a todos los idiomas ****/
 	    $common = new Zend_Form_SubForm();  
 	    $enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $enabled->setValue($c->enabled);
	    $common->addElement($enabled);	
	    $form->addSubForms(array("common" => $common));
	    /**** Datos por idioma *****/
 	    $langs = new Zend_Models_Lang();
	    $langs = $langs->getEnabledLangs();
	    foreach( $langs as $l)
	    {
	      $sub = new Zend_Form_SubForm();
	      $langCategory = new Zend_Models_LangClientCategory();
	      $langCategory = $langCategory->getCategoryByKey($c->id, $l['prefix2']);
	      $name = $sub->createElement("text","name_".$l['prefix2']);
		  $name->setAttrib("class", "text-long");
 	   	  $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
		  $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		  $name->setValue($langCategory['name']);
		  $sub->addElement($name);
		  $form->addSubForms(array($l['prefix2'] => $sub));
	    }
 	    /***************************/
 		$id = $form->createElement("hidden",'id_category');
		$id->setValue($c->id);
		$form->addElement($id);
		$form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
		return $form;
 }
 	
 	public function editcategoryAction()
	{
 		$form = $this->editCategoryForm();
 		$this->view->form = $form;	
 		$langs = new Zend_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
   	    
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
     	else
     	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
      	        $values = $form->getValues();
 				$category = new Zend_Models_ClientCategory();
				$category = $category->getCategoryById($values['id_category']);
				$category->enabled = $values['common']['enabled'];
				$category->save();
				$langs = new Zend_Models_Lang();
				$langs = $langs->getEnabledLangs();
				foreach( $langs as $l )
				{
				  $langCategory = new Zend_Models_LangClientCategory();
				  $langCategory = $langCategory->getCategoryById($values['id_category'], $l['prefix2']);
				  
				  if ( count( $langCategory ) <= 0 )
				  {
                    $langCategory = new Zend_Models_LangClientCategory();
				    $langCategory = $langCategory->createRow();
				  }  
				  $langCategory->id_category = $category->id;
				  $langCategory->lang = $l['prefix2'];
				  $langCategory->name = $values[$l['prefix2']]['name_'.$l['prefix2']];
				  $langCategory->save();
				}
				$this->_redirect("/admin/client");
		    }
 	    }
	}    
 	
 	public function listclientAction()
 	{
 		$request = $this->_request->getParams();
   		$category = $request['category']; 
    	$this->view->category = $category;
    	$lang = Common_Translate::getLanguage();
    	$c = new Zend_Models_LangClientCategory();
    	$c = $c->getCategoryById($category,$lang);
   		$this->view->categoryName = $c->name;
   		$content = new Zend_Models_Client();
 		$this->view->contents = $content->getClientByCategory($category);
 	}
 	
 	public function newclientAction()
 	{
	    $form = $this->addClientForm();
 		$this->view->form = $form;	
        $langs = new Zend_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
 		if (!$this->getRequest()->isPost())
     	{
     	  	return;
     	}
     	else
    	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				
				$content = new Zend_Models_Client();
				$content = $content->createRow();
				$content->id_category = $values['common']['category'];
				$content->enabled = $values['common']['enabled'];
				$content->name = $values['common']['name'];
				$content->last_name = $values['common']['last_name'];
				$content->email = $values['common']['email'];
				$content->phone = $values['common']['phone'];
				$content->address = $values['common']['address'];
				$content->id_country = $values['common']['country'];
				$content->state = $values['common']['state'];
				$content->city = $values['common']['city'];
				$content->zip_code = $values['common']['zip_code'];
				$content->save();
				$this->_redirect("/admin/client/listclient/category/".$values['common']['category']);
			}
		}
	 }
	 
	private function addClientForm()
	 {
		$form = new Zend_Form();
	 	$form->setAction("/admin/client/newclient")
	             ->setMethod('post');
    	$form->setAttrib("class", "jNice");         
        $lang = Common_Translate::getLanguage();             
    	$id_content = $this->_request->getParam('content');
        /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$category = $form->createElement("select","category");
		$categories = new Zend_Models_LangClientCategory();
		$categories = $categories->getCategoriesByLang($lang);	
		$category->setLabel('Categoría:');
		$category->addMultiOptions($this->formatCategories($categories)); 
 	    $enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $common->addElement($category);
	    $common->addElement($enabled);	

	    $name = $common->createElement("text","name");
		$name->setAttrib("class", "text-long");
 	   	$name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
 	   	$name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($name);
	    
	    $last_name = $common->createElement("text","last_name");
		$last_name->setAttrib("class", "text-long");
 	   	$last_name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Apellido:'))->setRequired(true);
 	   	$last_name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($last_name);
	    
	    $email = $common->createElement('text','email');
	    $email->setAttrib("class", "text-long");
	    $email->addValidator('emailAddress')->setLabel($this->view->t->_('E-mail'))->setRequired(true);
	    $email->addErrorMessage($this->view->t->_('Direccion_de_mail_no_valida'));
	    $common->addElement($email);
	    
	    $phone = $common->createElement("text","phone");
		$phone->setAttrib("class", "text-long");
 	   	$phone->addValidator('NotEmpty')->setLabel($this->view->t->_('*Telefono:'))->setRequired(true);
 	   	$phone->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($phone);
	    
	    $address = $common->createElement("text","address");
		$address->setAttrib("class", "text-long");
 	   	$address->addValidator('NotEmpty')->setLabel($this->view->t->_('*Direccion:'))->setRequired(true);
 	   	$address->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($address);
	    
	    $country = $common->createElement("select","country");
		$countries = new Zend_Models_Country();
		$countries = $countries->getAll();	
		$country->setLabel('*Pais:');
		$country->addMultiOptions($this->formatCountries($countries)); 
		$common->addElement($country);

		$state = $common->createElement("text","state");
		$state->setAttrib("class", "text-long");
 	   	$state->addValidator('NotEmpty')->setLabel($this->view->t->_('*Provincia:'))->setRequired(true);
 	   	$state->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($state);
		
	    $city = $common->createElement("text","city");
		$city->setAttrib("class", "text-long");
 	   	$city->addValidator('NotEmpty')->setLabel($this->view->t->_('*Ciudad:'))->setRequired(true);
 	   	$city->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($city);
		
	    $zip_code = $common->createElement("text","zip_code");
		$zip_code->setAttrib("class", "text-long");
 	   	$zip_code->addValidator('NotEmpty')->setLabel($this->view->t->_('*Codigo Postal:'))->setRequired(true);
 	   	$zip_code->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
	    $common->addElement($zip_code);
		
	    $form->addSubForms(array("common" => $common));
	    /**** Fin datos comunes ***/
	    $form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
	}
 	
	private function formatCountries($countries)
	{
	  	$options = array();
	
		foreach( $countries as $c )
		{
			$options[$c['id']] = $c['title'];
		}
		return $options;
	}
	private function formatCategories($categories)
	{
		$options = array();
	
		foreach( $categories as $c )
		{
			$options[$c['id']] = $c['name'];
		}
		return $options;
	}
	
 	public function editclientAction()
 	{
 		$form = $this->editClientForm();
 		$this->view->form = $form;	
   	    $langs = new Zend_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
    	 else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$client = new Zend_Models_Client();
				$client = $client->getClientById($values['id_client']);
				$client->enabled = $values['common']['enabled'];
				$client->id_category = $values['common']['category'];
				$client->name = $values['common']['name'];
				$client->last_name = $values['common']['last_name'];
				$client->email = $values['common']['email'];
				$client->phone = $values['common']['phone'];
				$client->address = $values['common']['address'];
				$client->state = $values['common']['state'];
				$client->id_country = $values['common']['country'];
				$client->city = $values['common']['city'];
				$client->zip_code = $values['common']['zip_code'];
				
				$client->save();
				
				$this->_redirect("/admin/client/listclient/category/".$values['common']['category']);
			}
		}
 	}
 	
	private function editClientForm()
	{
 		$form = new Zend_Form();
 		$form->setAction("/admin/client/editclient")
       	      ->setMethod('post');
    	$form->setAttrib("class", "jNice");   
    	$id_client = $this->_request->getParam('client');
        $lang = Common_Translate::getLanguage();    
    	$client = new Zend_Models_Client();  
    	$client = $client->getClientById($id_client);         
         /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$category = $form->createElement("select","category");
		$categories = new Zend_Models_LangClientCategory();
		$categories = $categories->getCategoriesByLang($lang);	
		$category->setLabel('Categoría:');
		$category->addMultiOptions($this->formatCategories($categories));
		$category->setValue($client->id_category); 
 	    $enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $enabled->setValue($client->enabled);
 	    $common->addElement($category);
	    $common->addElement($enabled);	
	    $form->addSubForms(array("common" => $common));
	    	    $name = $common->createElement("text","name");
		
	    $name->setAttrib("class", "text-long");
 	   	$name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
 	   	$name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$name->setValue($client->name);
	    $common->addElement($name);
	    
	    $last_name = $common->createElement("text","last_name");
		$last_name->setAttrib("class", "text-long");
 	   	$last_name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Apellido:'))->setRequired(true);
 	   	$last_name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$last_name->setValue($client->last_name);
	    $common->addElement($last_name);
	    
	    $email = $common->createElement('text','email');
	    $email->setAttrib("class", "text-long");
	    $email->addValidator('emailAddress')->setLabel($this->view->t->_('E-mail'))->setRequired(true);
	    $email->addErrorMessage($this->view->t->_('Direccion_de_mail_no_valida'));
	    $email->setValue($client->email);
	    $common->addElement($email);
	    
	    $phone = $common->createElement("text","phone");
		$phone->setAttrib("class", "text-long");
 	   	$phone->addValidator('NotEmpty')->setLabel($this->view->t->_('*Telefono:'))->setRequired(true);
 	   	$phone->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$phone->setValue($client->phone);
	    $common->addElement($phone);
	    
	    $address = $common->createElement("text","address");
		$address->setAttrib("class", "text-long");
 	   	$address->addValidator('NotEmpty')->setLabel($this->view->t->_('*Direccion:'))->setRequired(true);
 	   	$address->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$address->setValue($client->address);
	    $common->addElement($address);
	    
	    $country = $common->createElement("select","country");
		$countries = new Zend_Models_Country();
		$countries = $countries->getAll();	
		$country->setLabel('*Pais:');
		$country->addMultiOptions($this->formatCountries($countries));
		$country->setValue($client->id_country); 
		$common->addElement($country);

		$state = $common->createElement("text","state");
		$state->setAttrib("class", "text-long");
 	   	$state->addValidator('NotEmpty')->setLabel($this->view->t->_('*Provincia:'))->setRequired(true);
 	   	$state->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$state->setValue($client->state);
	    $common->addElement($state);
		
	    $city = $common->createElement("text","city");
		$city->setAttrib("class", "text-long");
 	   	$city->addValidator('NotEmpty')->setLabel($this->view->t->_('*Ciudad:'))->setRequired(true);
 	   	$city->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$city->setValue($client->city);
	    $common->addElement($city);
		
	    $zip_code = $common->createElement("text","zip_code");
		$zip_code->setAttrib("class", "text-long");
 	   	$zip_code->addValidator('NotEmpty')->setLabel($this->view->t->_('*Codigo Postal:'))->setRequired(true);
 	   	$zip_code->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
 	   	$zip_code->setValue($client->zip_code);
	    $common->addElement($zip_code);
		
	 	   	
	    $id = $form->createElement("hidden","id_client");
		$id->setValue($client->id);
    	$form->addElement($id);
     	$form->addElement('submit', 'add', array('label' => 'Aceptar'));
		return $form;
 }
 
 	public function deleteclientAction()
	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$client = $request['client'];
   	 	$category = $request['category'];
   	 	$c = new Zend_Models_Client();
   	 	$c = $c->getClientById($client);
    	$c->delete();
    	$this->_redirect("/admin/client/listclient/category/$category");
 	}
}    